package com.shopcart.controller;

import java.util.ArrayList;
import java.util.List;

import com.shopcart.entity.Customer;
import com.shopcart.entity.Product;
import com.shopcart.exception.CustomerExistsException;
import com.shopcart.exception.CustomerIsLoginedException;
import com.shopcart.exception.NoSuchCustomerExistsException;
import com.shopcart.exception.PasswordMismatchException;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

@Stateless(name = "CustomerControllerBean", mappedName = "ejb/CustomerControllerBean")
public class CustomerControllerBean implements CustomerController {
	@PersistenceContext
	private EntityManager em;

	@SuppressWarnings("unchecked")
	@Override
	public Customer loginCustomer(String email, String password)
			throws NoSuchCustomerExistsException,
			CustomerIsLoginedException, PasswordMismatchException {
		Query query = em.createQuery("select customer from Customer customer left join fetch customer.productsNotInStock where customer.email=:email");
		query.setParameter("email", email);
		List<Customer> customers = (List<Customer>) query.getResultList();

		if (customers.isEmpty())
			throw new NoSuchCustomerExistsException();

		Customer c = customers.get(0);
		if (c.isLogin()) {
			throw new CustomerIsLoginedException();
		}
		if (c.getPassword().equals(password)) {
			c.setLogin(true);
			return c;
		} else
			throw new PasswordMismatchException();

	}

	public void logoutCustomer(Customer customer) {
		if (customer != null) {
			customer.setLogin(false);
			em.merge(customer);
		}
	}

	@Override
	public Customer registerCustomer(String email, String password, String name, String surname) throws CustomerExistsException {
		Customer customer = new Customer();
		customer.setFirstName(name);
		customer.setLastName(surname);
		customer.setEmail(email);
		customer.setPassword(password);

		try {
			em.persist(customer);
		} catch (PersistenceException e) {
			throw new CustomerExistsException();
		}

		return customer;
	}

	@Override
	public Customer addNotExistingProduct(Customer customer, Long productId) {
		Query query = em.createQuery("select p from Product p where p.id=:id");
		query.setParameter("id", productId);
		Product product = (Product) query.getSingleResult();
		
		if (product != null) {
			List<Product> products = customer.getProductsNotInStock();
			
			if (!products.contains(product)) {
				products.add(product);
				customer.setProductsNotInStock(products);
				
				em.merge(customer);
			}
		}
		
		return customer;
	}

	@Override
	public Customer deleteNotExistingProduct(Customer customer, Long productId) {
		Query query = em.createQuery("select p from Product p where p.id=:id");
		query.setParameter("id", productId);
		Product product = (Product) query.getSingleResult();
		
		if (product != null) {
			List<Product> products = customer.getProductsNotInStock();
			
			List<Product> newProducts = new ArrayList<Product>();
			
			for (int i = 0; i < products.size(); i++) {
				if (!(products.get(i).getId().equals(product.getId()))) {
					newProducts.add(products.get(i));
				}
			}
			
			customer.setProductsNotInStock(newProducts);
			
			em.merge(customer);
		}
		
		return customer;
	}
}